const { Source } = require('@/models/model')
const { dumpCheck } = require('./dumpCheck')

module.exports.arrayToTree = (items) => {
  const result = []

  const map = {}

  items.forEach((item) => {
    map[item.parentLevel] = item
  })

  items.forEach((item) => {
    items.forEach((v) => {
      if (v.parentLevel.startsWith(item.parentLevel)) {
      }
    })
  })

  return result
}

module.exports.uniqueArrayByKeyWithCount = (array, key) => {
  const countMap = {}

  // 统计每个对象的出现次数
  array.forEach((item) => {
    const keyValue = item[key]
    if (countMap[keyValue]) {
      countMap[keyValue]++
    } else {
      countMap[keyValue] = 1
    }
  })

  // 构建去重后的数组，并添加 cnt 属性
  return array.filter((item, index, self) => {
    const keyValue = item[key]
    if (index === self.findIndex((t) => t[key] === keyValue)) {
      item.dataValues.questionCnt = countMap[keyValue]
      return true
    }
    return false
  })
}
